/*
 * Copyright (c) 2012-2013, Charles Santos da Silva <silva.charlessantos@gmail.com> -
 * scharlessantos.org
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE
 */

package org.scharlessantos.hermes;

import org.scharlessantos.hermes.writer.HFilter;
import org.scharlessantos.hermes.writer.HLevel;
import org.scharlessantos.hermes.writer.HWriter;

/**
 * 
 * @author Charles Santos da Silva
 */
/* default */class HLogger {
	
	private final HWriter writer;
	private final HFilter filter;
	
	/**
	 * Defines that logger and filter of log will be used
	 * 
	 * @param writer Hermes Logger Writer will be used
	 * @param filter Hermes Logger Filter will be used
	 * @see HWriter
	 * @see HFilter
	 */
	public HLogger(HWriter writer, HFilter filter) {
		this.writer = writer;
		this.filter = filter;
	}
	
	/**
	 * Log a message if filter allow
	 * 
	 * @param level Level of message log
	 * @param source message log origin
	 * @param message message will be logged
	 */
	public final void log(HLevel level, StackTraceElement source, String message) {
		if (filter != null && !filter.shouldLogMessage(level, source, message))
			return;
		
		writer.logMessage(level, source, message);
	}
	
	/**
	 * Log a exception if filter
	 * 
	 * @param level Level of exception log
	 * @param source exception log origin
	 * @param exception exception will be logged
	 */
	public final void log(HLevel level, StackTraceElement source, Throwable exception) {
		if (filter != null && !filter.shouldLogException(level, source, exception))
			return;
		
		writer.logException(level, source, exception);
	}
	
	@Override
	public int hashCode() {
		final int prime = 33;
		int result = prime + ((filter == null) ? 0 : filter.hashCode());
		result *= prime + ((writer == null) ? 0 : writer.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof HLogger))
			return false;
		HLogger other = (HLogger) obj;
		if (filter == null) {
			if (other.filter != null)
				return false;
		} else if (!filter.equals(other.filter))
			return false;
		if (writer == null) {
			if (other.writer != null)
				return false;
		} else if (!writer.equals(other.writer))
			return false;
		
		return true;
	}
	
}
